package com.oazon.moco.link.serialport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.oazon.common.Logger;
import com.oazon.moco.bean.VolumeChannel;
import com.oazon.moco.lan.core.http.bean.Volume;
import com.oazon.moco.lan.core.http.bean.VolumeInstance;
import com.oazon.moco.link.core.LinkEvent;
import com.oazon.moco.link.core.LinkEventController;
import com.oazon.moco.link.core.LinkEventListener;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LinkEventSource;
import com.oazon.moco.link.event.PlayerEvent;
import com.oazon.moco.link.event.PlayerStatus;
import com.oazon.moco.serialport.SerialSend;

/**
 * 音量加减在这个类中实现，初始音量通过LinkSDK.curVolume获得
 * 
 * 每次加减完音量用 LinkSDK.getInstance().setVolume(new LinkEventSource(),
 * LinkSDK.curVolume); 通知更方音量值的改变
 * 
 * 这个类不过滤事件源
 * 
 * @author Anty
 * @author Gavin
 *
 */
// TODO　Anty

public class PlayerEventListener extends LinkEventListener {
	public PlayerEventListener(LinkEventSource... sources) {
		setBlackList(Arrays.asList(sources));
	}

	
	private final int INTERVAL = 6;
	private int speed = 0;
	private final int MESSAGE_RESET_SPEED = 0;
	private static int lastChange = 0;//-1 减小，0初始值，1增大
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case MESSAGE_RESET_SPEED:
					resetSpeed();
			}
		}
	};

	private void resetSpeed() {
		lastChange = 0;
		speed = 0;
	}

	private int getUpVolume() {
		
		if(lastChange != 1){
			resetSpeed();
		}
		lastChange = 1;
		
		int reslut = calVolume(true);
		if (reslut > 31) {
			reslut = 31;
			resetSpeed();
		}
		return reslut;
	};

	private int getDownVolume() {
		
		if(lastChange != -1){
			resetSpeed();
		}
		lastChange = -1;
		
		int reslut = calVolume(false);
		if (reslut < 0) {
			reslut = 0;
			resetSpeed();
		}
		return reslut;
	};

	private int calVolume(boolean up){
		int reslut = LinkSDK.curVolume;
		int grap_count = speed / INTERVAL;
		Logger.i("PlayerEventListener", "speed=="+speed+"grap=="+grap_count);
		if (grap_count <= 2 && speed % INTERVAL == 0) {
			Logger.i("PlayerEventListener", "change level 1");
			if(up){
				reslut = LinkSDK.curVolume + 1;	
			}else{
				reslut = LinkSDK.curVolume - 1;
			}
		} else if(grap_count > 2 && grap_count <= 5  /*&& speed % (INTERVAL/INTERVAL) == 0*/) {
			Logger.i("PlayerEventListener", "change level 2");
			if(up){
				reslut = LinkSDK.curVolume + 2;	
			}else{
				reslut = LinkSDK.curVolume - 2;
			}
		} /*else if(grap_count > 6 && grap_count <= 9 && speed % (INTERVAL/INTERVAL) == 0) {
			if(up){
				reslut = LinkSDK.curVolume + 3;	
			}else{
				reslut = LinkSDK.curVolume - 3;
			}
		} */else if(grap_count > 5){
			Logger.i("PlayerEventListener", "change level 3");
			if(up){
				reslut = LinkSDK.curVolume + 4;	
			}else{
				reslut = LinkSDK.curVolume - 4;
			}
		}
		speed ++;
		return reslut;
	}
	
	private void resetSpeedDelay(int delay) {
		mHandler.removeMessages(MESSAGE_RESET_SPEED);
		Message msg = mHandler.obtainMessage(MESSAGE_RESET_SPEED);
		mHandler.sendMessageDelayed(msg, delay);
	}

	@Override
	public void onLinkEvent(LinkEvent event) {
		if (filter((LinkEventSource) event.getSource())
				|| !(event instanceof PlayerEvent)) {
			return;
		}
		PlayerEvent e = (PlayerEvent) event;
		int volume = 0;
		if(e.getVolumeInstance() != null && e.getType().equals(PlayerStatus.VOLUME)){
			List<Volume> volumes = e.getVolumeInstance().getVolume();
			for (Volume volume2 : volumes) {
				if(volume2.getChannel().equals(VolumeChannel.MASTER)){
					volume = volume2.getVal();
				}
			}
		}
		if (volume >= 0 && e.getType().equals(PlayerStatus.VOLUME)) {
			SerialSend.getInstance().sendVolume(volume);
			LinkSDK.curVolume = volume;
			Log.d("volume", "volume===" + volume);
		} else if (e.getType().equals(PlayerStatus.VOLUME_UP)) {
			if (LinkSDK.curVolume <= 31) {
				if(LinkSDK.curVolume != 31){
					LinkSDK.curVolume = getUpVolume();
					resetSpeedDelay(1000);
				}
				Log.d("volume", "VOLUME_UP====" + LinkSDK.curVolume);
				sendVolume();
			}
		} else if (e.getType().equals(PlayerStatus.VOLUME_DOWN)) {
			if (LinkSDK.curVolume >= 0) {
				if(LinkSDK.curVolume != 0){
					LinkSDK.curVolume = getDownVolume();
					resetSpeedDelay(1000);
				}
				Log.d("volume", "VOLUME_DOWN====" + LinkSDK.curVolume);
				sendVolume();
			}
		}
	}
	
	private void sendVolume(){
		VolumeInstance volumeInstance = new VolumeInstance();
		List<Volume> volumes = new ArrayList<Volume>();
		Volume volumeObj = new Volume();
		volumeObj.setChannel(VolumeChannel.MASTER);
		volumeObj.setVal(LinkSDK.curVolume);
		volumes.add(volumeObj);
		volumeInstance.setVolume(volumes);
		LinkSDK.getInstance().setVolume(
				LinkEventController.serialSource, volumeInstance);
	}
	
}
